package farmers.game;

import java.io.*;
import java.util.Map;
import java.util.TreeMap;

public class Moli_enemy_Test {

	public static void main(String[] args) throws Exception {
		// 文件夹路径
		String path = "D:\\001_Server\\2020.gmsv\\gmsv\\data\\enemy.txt";
		TreeMap<String, String> map = readFile(path);

		for (Map.Entry<String, String> entry: map.entrySet()) {
			System.out.println(entry.getKey() + "----------" + entry.getValue());
			writeFileEnd("D:\\enemy.txt", entry.getValue());
		}

//		System.out.println(result);
		
//		writeFile(path, result.toString());
	}

	public static void writeFile(String fileName, String fileContent) {
		try {
			File f = new File(fileName);
			if (!f.exists()) {
				f.createNewFile();
			}
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "gbk");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(fileContent);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 追加内容
	public static void writeFileEnd(String fileName, String fileContent) {
		FileWriter fw = null;
		try {
			//如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File(fileName);
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println(fileContent);
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 读取一个文本 一行一行读取
	 */
	public static TreeMap<String, String> readFile(String path) throws IOException {
		// 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
		TreeMap map = new TreeMap<String, String>();

		FileInputStream fis = new FileInputStream(path);
		// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
		InputStreamReader isr = new InputStreamReader(fis, "GB2312");
		BufferedReader br = new BufferedReader(isr);
		String line = "";
		while ((line = br.readLine()) != null) {
			// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
			if(line.startsWith("#") || line.equals("")){
				continue;
			}

//			System.out.println(line);

			String[] split = line.split("\t");

			// 查看id位置
			String seq_context = "";
			for (int i = 0; i < split.length; i++) {

				seq_context += i + "=" + split[i] + ", ";
			}
			System.out.println(seq_context);




			String key = String.format("%09d", Integer.valueOf(split[2]));
			map.put(key, line);




		}
		br.close();
		isr.close();
		fis.close();
		return map;
	}
}
